package cumeo89.chii.client.entry.editor;

import com.google.gwt.user.client.ui.MultiWordSuggestOracle;

import cumeo89.chii.client.util.JSStringArray;
import cumeo89.chii.client.util.MyArrays;

class FixedSuggestEditor extends SuggestEditor {

	private JSStringArray codes, names;

	public FixedSuggestEditor(JSStringArray codes, JSStringArray names) {
		this.codes = codes;
		this.names = names;
		MultiWordSuggestOracle suggestOracle = new MultiWordSuggestOracle();
		for (int i = 0; i < codes.length(); i++) {
			suggestOracle.add(codes.get(i) + " - " + names.get(i));
		}
		initEditor(suggestOracle);
	}

	@Override
	protected String format(String value) {
		String ret = MyArrays.translate(value, codes, names);
		return ret == null ? "" : ret;
	}

	@Override
	protected String parse(String value) {
		int pos = value.lastIndexOf('-');
		while (value.charAt(pos - 1) == ' ')
			pos--;
		return pos < 0 ? "" : value.substring(0, pos);
	}

}
